﻿using System.IO;
using Adventure.Server.Data;
using Adventure.Server.Library;
using Adventure.Tests.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Adventure.Tests
{
    /// <summary>
    /// Tests the SavedObject class.
    /// </summary>
    [TestClass]

    public class TextFileDataAdapterTests
    {

        /// <summary>
        /// Gets or sets the test context.
        /// </summary>
        public TestContext TestContext { get; set; }

        /// <summary>
        /// Gets or sets the temporary folder for the tests.
        /// </summary>
        public static string TempFolder { get; set; }

        /// <summary>
        /// Initialize the Tests in this Class.
        /// </summary>
        [ClassInitialize]
        public static void Init(TestContext testContext)
        {
            // Get a temporary folder, remove it if it exists and create it afterwards.
            TempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var directoryInfo = new DirectoryInfo(TempFolder);
            if (directoryInfo.Exists) directoryInfo.Delete(true);
            directoryInfo.Create();
        }

        /// <summary>
        /// Cleanup after the tests.
        /// </summary>
        [ClassCleanup]
        public static void CleanUp()
        {
            var directoryInfo = new DirectoryInfo(TempFolder);
            if (directoryInfo.Exists) directoryInfo.Delete(true);
        }

        /// <summary>
        /// Tests the saving of Objects.
        /// </summary>
        [TestMethod]
        public void SaveObjectTest()
        {
            // Create a TextFileDataAdapter
            var adapter = CreateTextFileDataAdapter();

            // Create an object and save it with the adapter.
            var id = Settings.Default.TestObjectId;
            SavedObject obj;
            using (obj = SavedObject.Create(HelperFunctions.CreateAttributes(id)))
            {
                obj.Save(adapter);
            }

            // Check the Reference Counter of SavedObjects
            Assert.AreEqual(0, obj.ReferenceCounter);
        }

        /// <summary>
        /// Test the use of / inside a Windows directory.
        /// </summary>
        [TestMethod]
        public void SaveAndLoadSomeObjectAttributesTest()
        {
            // Create a TextFileDataAdapter
            var adapter = CreateTextFileDataAdapter();

            // Id to use for our test
            var id = Settings.Default.TestSaveAndLoadObjectId;

            // Create attributes to save
            var attrib1 = HelperFunctions.CreateAttributes(id);
            attrib1["test"] = "Just a test!";
            attrib1["MultiLineTest"] = "Just\r\na\r\ntest!";

            // Save the object
            adapter.SaveObjectAttributes(attrib1);

            // Check that the file is there
            var file = Path.Combine(TempFolder, id + ".txt");
            var fileInfo = new FileInfo(file);
            Assert.IsTrue(fileInfo.Exists);

            // Load attributes again
            var attrib2 = adapter.GetObjectAttributes(id);
            Assert.IsTrue(attrib1.Equals(attrib2));
        }

        /// <summary>
        /// Create a TextFileDataAdapter.
        /// </summary>
        /// <returns>A TextFileDataAdapter.</returns>
        public static IDataAdapter CreateTextFileDataAdapter()
        {
            // Create a TextFileDataAdapter
            var adapter = new TextFileDataAdapter();
            adapter.SetSource(TempFolder);
            return adapter;
        }
    }
}
